package com.itheima.bos.web.action;

import com.opensymphony.xwork2.ActionSupport;
import org.activiti.engine.ProcessEngine;
import org.activiti.engine.repository.DeploymentBuilder;
import org.activiti.engine.repository.ProcessDefinition;
import org.activiti.engine.repository.ProcessDefinitionQuery;
import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.List;
import java.util.zip.ZipInputStream;

/**
 * Created by Mr.King on 2017/3/20.
 */
@Controller
@Scope("prototype")
public class ProcessDefinitionAction extends ActionSupport{

    //注入流程引擎對象
    @Autowired
    private ProcessEngine processEngine;


    /**
     * 查询流程定义
     * @return
     */
    public String list(){
        ProcessDefinitionQuery query = processEngine.getRepositoryService().createProcessDefinitionQuery();
        query.latestVersion();
        List<ProcessDefinition> list = query.list();
        ServletActionContext.getContext().getValueStack().set("list",list);
        return "list";
    }

    //文件上传项
    private File deploy;
    public void setDeploy(File deploy) {
        this.deploy = deploy;
    }

    /**
     * 接收文件上传，保存流程定义
     */
    public String deploy() throws FileNotFoundException {
        DeploymentBuilder deployment = processEngine.getRepositoryService().createDeployment();

        InputStream fileInputStream = new FileInputStream(deploy);
        deployment.addZipInputStream(new ZipInputStream(fileInputStream));
        deployment.deploy();
        return "toList";
    }

    //流程id
    private String id;
    public void setId(String id) {
        this.id = id;
    }

    /**
     * 查看流程图
     */
    public String viewpng(){
        InputStream inputStream = processEngine.getRepositoryService().getProcessDiagram(id);
        ServletActionContext.getContext().getValueStack().set("inputStream",inputStream);
        return "viewpng";
    }
}
